<?php

namespace App\Models;

use App\Libs\Helper\TreeHelper;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\ProductCategory
 *
 * @property integer $id
 * @property integer $parent_id
 * @property string $name
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property-read \App\Models\ProductCategory $parentCategory
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductCategory[] $childrenCategories
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ProductCategory whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ProductCategory whereParentId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ProductCategory whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ProductCategory whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\ProductCategory whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class ProductCategory extends Model
{

    use TreeHelper;

    /**
     * 可以被批量賦值的屬性。
     *
     * @var array
     */
    protected $fillable = ['name', 'parent_id'];


    /**
     * 获取父级分类
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parentCategory()
    {
        return $this->belongsTo('App\Models\ProductCategory', 'parent_id');
    }

    /**
     * 获取子分类
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function childrenCategories()
    {
        return $this->hasMany('App\Models\ProductCategory', 'parent_id');
    }

}
